﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;

namespace ParticleFiltersDemo1
{
    public partial class MainPage : UserControl
    {
        Controller controller;
        DispatcherTimer timer;

        Canvas layoutRoot;
        int stepSize = 50;
        bool isReady = false;

        public MainPage()
        {
            InitializeComponent();

            layoutRoot = FindName( "LayoutRoot" ) as Canvas;
            this.controller = new Controller();
            this.timer = new DispatcherTimer();
            this.timer.Interval = TimeSpan.FromSeconds( 2 );
            this.timer.Tick += new EventHandler( timer_Tick );

            this.DataContext = this.controller;

            DrawLines();
            DrawObstacles();
            this.timer.Start();
                    
            this.MouseLeftButtonUp += new MouseButtonEventHandler( MainPage_MouseLeftButtonUp );
        }

        void MainPage_MouseLeftButtonUp( object sender, MouseButtonEventArgs e )
        {
            if ( this.timer.IsEnabled == true )
            {
                this.timer.Stop();
            }
            else
            {
                this.timer.Start();
            }
        }
        
        private void timer_Tick( object sender, EventArgs e )
        {
            if ( isReady == true )
                return;

            isReady = this.controller.DoFiltering();

            //set coordinates of the mean point 
            Canvas.SetLeft( MeanPoint, this.controller.Particles.meanX * 50 + 15 );
            Canvas.SetTop( MeanPoint, this.controller.Particles.meanY * 50 + 15 );
        }

        void DrawLines()
        {
            SolidColorBrush stroke = new SolidColorBrush();

            Board board = new Board();
            Point previousPoint = new Point( 10, 10 );
            Point newPoint = new Point( board.sizeX * stepSize, 10 );
            for ( int i = 0; i < board.sizeX + 1; i++ )
            {
                Line line = new Line();
                line.X1 = previousPoint.X;
                line.Y1 = previousPoint.Y;
                line.X2 = newPoint.X;
                line.Y2 = newPoint.Y;
                previousPoint.Y += stepSize;
                newPoint.Y += stepSize;
                line.StrokeThickness = 1;
                stroke.Color = Colors.Black;
                line.Stroke = stroke;
                line.SetValue( Canvas.ZIndexProperty, ( object )( -1 ) );

                layoutRoot.Children.Add( line );
            }
            previousPoint = new Point( 10, 10 );
            newPoint = new Point( 10, board.sizeY * stepSize );
            for ( int i = 0; i < board.sizeY + 1; i++ )
            {
                Line line = new Line();
                line.X1 = previousPoint.X;
                line.Y1 = previousPoint.Y;
                line.X2 = newPoint.X;
                line.Y2 = newPoint.Y;
                previousPoint.X += stepSize;
                newPoint.X += stepSize;
                line.StrokeThickness = 1;
                stroke.Color = Colors.Black;
                line.Stroke = stroke;
                line.SetValue( Canvas.ZIndexProperty, ( object )( -1 ) );

                layoutRoot.Children.Add( line );
            }
        }

        void DrawObstacles()
        {
            Board board = new Board();
            for ( int i = 0; i < board.sizeX; i++ )
            {
                for ( int j = 0; j < board.sizeY; j++ )
                {
                    if ( board.board[i, j] == 1 )
                    {
                        DrawSquare( i * stepSize + 10, j * stepSize + 10 );
                    }
                }
            }
        }

        void DrawSquare( int x, int y )
        {

            Rectangle rect = new Rectangle();
            Point origPoint = new Point( x, y );
            //   origPoint = e.GetPosition(draw);
            Canvas.SetLeft( rect, origPoint.X );
            Canvas.SetTop( rect, origPoint.Y );
            rect.Stroke = new SolidColorBrush( Colors.Purple );
            rect.Fill = new SolidColorBrush( Colors.Black );
            // rect.StrokeThickness = 2;
            rect.Height = stepSize;
            rect.Width = stepSize;
            rect.SetValue( Canvas.ZIndexProperty, ( object )( -1 ) );

            layoutRoot.Children.Add( rect );
        }

        private void btn_Click( object sender, RoutedEventArgs e )
        {
            this.timer.Start();
        }
    }
}